import *  as THREE from "three"
import {v1, v2, v3, v4, roomConfig} from "./roomJson"

// export const textureBaseUrl = "../../../../public/texture/"
export const textureBaseUrl = import.meta.env.VITE_3D_STARIC_URL

/**创建房间
 * 
 * @returns 房间基本组成结构的模型列表 
 */
export function createRoom() {
    let meshList:Array<THREE.Mesh> = []
    const wall_texture = new THREE.TextureLoader().load( `${textureBaseUrl}wall/wall6.jpg` );
    const glass_texture = new THREE.TextureLoader().load( `${textureBaseUrl}wall/glass2.png` );
    // const glass_texture = new THREE.TextureLoader().load( `${textureBaseUrl}wall/glass-wall4.png` );
    glass_texture.wrapS = THREE.RepeatWrapping;
    glass_texture.wrapT = THREE.RepeatWrapping;
    glass_texture.repeat.set(5, 1); // 根据需要设置重复次数
    // glass_texture.repeat.set(2, 1); // 根据需要设置重复次数
    const floor_texture = new THREE.TextureLoader().load( `${textureBaseUrl}wall/top3.jpg` );
    floor_texture.wrapS = THREE.RepeatWrapping;
    floor_texture.wrapT = THREE.RepeatWrapping;
    floor_texture.repeat.set(10, 10); // 根据需要设置重复次数


    const wall_material = new THREE.MeshBasicMaterial({ map:wall_texture, transparent: true })
    const glass_material = new THREE.MeshBasicMaterial({ map:glass_texture, transparent: true })
    const floor_material = new THREE.MeshBasicMaterial({ map:floor_texture, transparent: true })
    // const floor_material = new THREE.MeshStandardMaterial({ map:floor_texture, transparent: true })
    

    roomConfig.forEach(config=>{
        let geometry = new THREE.BoxGeometry(config.x_len, config.y_len, config.z_len)
        let material
        if(config.type==="wall") {
            material = wall_material
        } else if (config.type === "glass") {
            material = glass_material
        } else {
            material = floor_material
        }
        let mesh = new THREE.Mesh(geometry, material)
        mesh.receiveShadow = true
        mesh.position.set(config.position.x,config.position.y,config.position.z)
        mesh.name = config.name
        meshList.push(mesh)
    })
    let doors = createDoor()
    return [...meshList, ...doors]
}

function createDoor() {
    const left_texture = new THREE.TextureLoader().load( `${textureBaseUrl}doorleft.jpg` );
    const right_texture = new THREE.TextureLoader().load( `${textureBaseUrl}doorright.jpg` );

    const left_material = new THREE.MeshStandardMaterial({ map:left_texture, transparent: true })
    left_material.side = THREE.DoubleSide
    const right_material = new THREE.MeshStandardMaterial({ map:right_texture, transparent: true })
    right_material.side = THREE.DoubleSide

    const left_geometry = new THREE.PlaneGeometry(100, 200)
    const right_geometry = new THREE.PlaneGeometry(100, 200)

    let left_door = new THREE.Mesh(left_geometry, left_material)
    let right_door = new THREE.Mesh(right_geometry, right_material)

    left_door.position.set(-50, -1 * (v3-200)/2, (v2+v4)/2 + 0.2)
    right_door.position.set(50, -1 * (v3-200)/2, (v2+v4)/2 + 0.2)

    let left_door_2 = left_door.clone()
    let right_door_2 = left_door.clone()

    left_door_2.position.set(-50, -1 * (v3-200)/2, (v2-v4)/2 - 0.2)
    right_door_2.position.set(50, -1 * (v3-200)/2, (v2-v4)/2 - 0.2)


    return [left_door, right_door, left_door_2, right_door_2]
}



export const rittalSize = {
    width: 70, //机柜的宽度
    height: 220, //机柜的高度
    len: 90, // 机柜的长度
}
export const bitConfig = {
    ...rittalSize,
    height: 3, //高度
    bottom: 10, // 距离底部的距离
    margin: 2, //间隔
}
const defLayout = {
    ...rittalSize,
    num: 8, //每行最多有多少台
    space: 10, //行内间距
    row_space: 50, // 行间距
}
/**创建机柜 */
export function createRittal(list:Array<any>, config:Object={}) {
    const layout_config = {...defLayout, ...config, ...rittalSize}
    let rittal_list:Array<THREE.Mesh> = []
    const front_texture = new THREE.TextureLoader().load( `${textureBaseUrl}rackSkin/rack_fore.jpg` );
    // front_texture.encoding
    const after_texture = new THREE.TextureLoader().load( `${textureBaseUrl}rackSkin/rack_Back.jpg` );
    const aside_texture = new THREE.TextureLoader().load( `${textureBaseUrl}rackSkin/rack_Side2.jpg` );
    const top_texture = new THREE.TextureLoader().load( `${textureBaseUrl}rackSkin/rack_Top.jpg` );


    let geometry = new THREE.BoxGeometry(layout_config.width, layout_config.height, layout_config.len)

    // y轴的偏移量
    const y = -1 * (v3 - layout_config.height)/2
    // x轴上的总长度
    const all_x = layout_config.num * (layout_config.width + layout_config.space) - layout_config.space
    // x轴的起始坐标
    const start_x = -1 * (all_x) / 2
    // z轴上的总长度
    const row_num = Math.ceil(list.length / layout_config.num)
    const all_z = row_num * (layout_config.len + layout_config.row_space) - layout_config.row_space

    // z轴的起始坐标
    const start_z = -1 * (all_z) / 2 


    list.forEach((ele, index)=>{
        const front_material = new THREE.MeshStandardMaterial({ map: front_texture, transparent:true, opacity:1 })
        const after_material = new THREE.MeshStandardMaterial({ map: after_texture, transparent:true, opacity:1 })
        const aside_material = new THREE.MeshStandardMaterial({ map: aside_texture, transparent:true, opacity:1 })
        const top_material = new THREE.MeshStandardMaterial({ map: top_texture, transparent:true, opacity:1 })
        let mesh = new THREE.Mesh(geometry, [top_material, top_material, aside_material, aside_material, front_material, after_material])
        mesh.userData = {
            ...ele
        }
        mesh.name = "机柜"
        rittal_list.push(mesh)
        const col_index = (index) % layout_config.num
        const row_index = Math.ceil((index+1) / layout_config.num)

        let px = col_index * (layout_config.width + layout_config.space) - layout_config.space + start_x
        let pz = row_index * (layout_config.len + layout_config.row_space) - layout_config.row_space + start_z
        
        mesh.position.set(px, y , pz)
    })

    return rittal_list
}


/**创建单个机柜及机柜内的详细陈设 */
export function createRittalInfo(info:any) {
    const aside_texture = new THREE.TextureLoader().load( `${textureBaseUrl}signRittal/aside.jpg` );
    const left_texture = new THREE.TextureLoader().load( `${textureBaseUrl}signRittal/left.png` );
    const right_texture = new THREE.TextureLoader().load( `${textureBaseUrl}signRittal/right.png` );
    const all_texture = new THREE.TextureLoader().load( `${textureBaseUrl}signRittal/all.png` );
    const bit_texture_entity = new THREE.TextureLoader().load( `${textureBaseUrl}server2.jpg` );

    const aside_material = new THREE.MeshStandardMaterial({ map: aside_texture, transparent: true })
    aside_material.side = THREE.DoubleSide
    const left_material = new THREE.MeshStandardMaterial({ map: left_texture, transparent: true })
    left_material.side = THREE.DoubleSide
    const right_material = new THREE.MeshStandardMaterial({ map: right_texture, transparent: true })
    right_material.side = THREE.DoubleSide
    const all_material = new THREE.MeshStandardMaterial({ map: all_texture, transparent: true })
    all_material.side = THREE.DoubleSide

    let group = new THREE.Group()
    // 创建四周的机箱壁
    const top_geometry = new THREE.PlaneGeometry(rittalSize.width, rittalSize.len)
    const left_geometry = new THREE.PlaneGeometry(rittalSize.height, rittalSize.len)
    const top_mesh = new THREE.Mesh(top_geometry, aside_material)
    const left_mesh = new THREE.Mesh(left_geometry, aside_material)
    top_mesh.position.set(0, rittalSize.height/2, 0)
    top_mesh.rotation.x = Math.PI / 2 
    left_mesh.position.set(-1 * rittalSize.width/2 , 0, 0)
    left_mesh.rotation.x = Math.PI / 2 
    left_mesh.rotation.y = Math.PI / 2
    let right_mesh = left_mesh.clone()
    let bottom_mesh = top_mesh.clone()
    right_mesh.position.set(rittalSize.width/2 , 0, 0)
    bottom_mesh.position.set(0, -1 * rittalSize.height/2, 0)

    // 创建前后门（可打开）
    const front_geometry = new THREE.PlaneGeometry(rittalSize.width, rittalSize.height)
    const after_geometry = new THREE.PlaneGeometry(rittalSize.width, rittalSize.height)
    const front_mesh = new THREE.Mesh(front_geometry, left_material)
    front_mesh.name = "前门"
    const after_mesh = new THREE.Mesh(after_geometry, right_material)
    after_mesh.name = "后门"
    front_mesh.position.set(0, 0, rittalSize.len/2)
    after_mesh.position.set(0, 0, -1 * rittalSize.len/2)

    // 创建机箱内部的两根支架
    let block_material = new THREE.MeshBasicMaterial({ color: 0x000000, transparent: true })
    let writh_material = new THREE.MeshBasicMaterial({ color: 0xffffff, transparent: true })
    const config = {
        block: { x: 1, y: 4, z: 8 },
        writh: { x: 5, y: 1, z: 1 }
    }
    let block_geometar = new THREE.BoxGeometry(config.block.x, config.block.y, config.block.z)
    let writh_geometar = new THREE.BoxGeometry(config.writh.x, config.writh.y, config.writh.z)

    // 前左
    let block_mesh = new THREE.Mesh(block_geometar, block_material)
    block_mesh.position.set(-1 * (rittalSize.width-config.block.x) / 2 + 1, 0, (rittalSize.len - config.block.z) / 2 - 1 )
    let writh_mesh = new THREE.Mesh(writh_geometar, writh_material)
    writh_mesh.position.set(-1 * (rittalSize.width-config.writh.x) / 2 + 1, 0, (rittalSize.len - config.block.z) / 2 - 1)
    // 前右
    let block2 = block_mesh.clone()
    block2.position.set((rittalSize.width - config.block.x) / 2 - 1, 0, (rittalSize.len - config.block.z) / 2 - 1 )
    let writh2 = block_mesh.clone()
    writh2.position.set((rittalSize.width - config.writh.x) / 2 - 1, 0, (rittalSize.len - config.block.z) / 2 - 1)
    // 后左
    let block3 = block_mesh.clone()
    block3.position.set(-1 * (rittalSize.width - config.block.x) / 2 + 1, 0, -1 * (rittalSize.len - config.block.z) / 2 + 1 )
    let writh3 = block_mesh.clone()
    writh3.position.set(-1 * (rittalSize.width - config.writh.x) / 2 + 1, 0, -1 * (rittalSize.len - config.block.z) / 2 + 1)
    // 后右
    let block4 = block_mesh.clone()
    block4.position.set((rittalSize.width - config.block.x) / 2 - 1, 0, -1 * (rittalSize.len - config.block.z) / 2 + 1 )
    let writh4 = block_mesh.clone()
    writh4.position.set((rittalSize.width - config.writh.x) / 2 - 1, 0, -1 * (rittalSize.len - config.block.z) / 2 + 1)


    // 添加u位标尺
    const bit_texture_01 = new THREE.TextureLoader().load( `${textureBaseUrl}signRittal/rack2.png` );
    const bit_texture_02 = new THREE.TextureLoader().load( `${textureBaseUrl}signRittal/rack1.png` );
    const bit_material_01 = new THREE.MeshStandardMaterial({ map: bit_texture_01, transparent: true })
    bit_material_01.side = THREE.DoubleSide
    const bit_material_02 = new THREE.MeshStandardMaterial({ map: bit_texture_02, transparent: true })
    bit_material_02.side = THREE.DoubleSide
    let plane_width = 4
    const plane_geometry = new THREE.PlaneGeometry(plane_width, rittalSize.height)
    let bit_mesh_01 = new THREE.Mesh(plane_geometry, bit_material_01)
    let bit_mesh_02 = new THREE.Mesh(plane_geometry, bit_material_02)
    bit_mesh_01.position.set((rittalSize.width - plane_width)/2 - config.writh.x , 0 , (rittalSize.len) / 2 - 1 )
    bit_mesh_02.position.set(-1 * (rittalSize.width - plane_width)/2 + config.writh.x , 0 , (rittalSize.len) / 2 - 1 )

    let bit_mesh_03 = bit_mesh_01.clone()
    bit_mesh_03.position.set((rittalSize.width - plane_width)/2 - config.writh.x , 0 , -1 * (rittalSize.len) / 2 + 1 )
    let bit_mesh_04 = bit_mesh_02.clone()
    bit_mesh_04.position.set(-1 * (rittalSize.width - plane_width)/2 + config.writh.x , 0 , -1 * (rittalSize.len) / 2 + 1 )


    // 创建u位信息
    const bitDataList:any[] = info.assetList||[]
    // 每个u位的尺寸（没有间隔）
    const bit_height = (rittalSize.height) / 45
    const bit_width = rittalSize.width - config.block.x * 2
    const bit_len = rittalSize.width - 4
    let bit_material:any = new THREE.MeshBasicMaterial({ color: 0x999999, transparent: true, opacity: 0.6 })
    // let bit_geometry = new THREE.BoxGeometry(bit_width, bit_height, bit_len)
    // let bit_mesh = new THREE.Mesh(bit_geometry, bit_material)
    const space_mesh_list:any[] = []
    const fille_mesh_list:any[] = []
    for(let i = 1; i <= 43; i++) {
        let loop = bitDataList.find(ele=>{ return (i >= Number(ele.assetBeginU) && i < Number(ele.assetEndU)) })
        if(!loop) {
            space_mesh_list.push({ start: i, len: 1, space: true })
        }
    }
    bitDataList.forEach(ele=>{
        let len_len = Number(ele.assetEndU) - Number(ele.assetBeginU)
        fille_mesh_list.push({start: Number(ele.assetBeginU), len: len_len, space: false, info:{...ele}})
    })
    let all_bit_list = [...fille_mesh_list,...space_mesh_list]
    all_bit_list.forEach(ele=>{
        let tempHeight = ele.len * bit_height
        let bit_geometry = new THREE.BoxGeometry(bit_width, tempHeight, bit_len)
        let mesh = new THREE.Mesh(bit_geometry, bit_material)
        if(!ele.space) {
            let temp_material = new THREE.MeshStandardMaterial({ map:bit_texture_entity, transparent:true, opacity: 0.8 })
            temp_material.side = THREE.DoubleSide 
            mesh.material = temp_material
            mesh.name = "U位"
            mesh.userData = {...ele.info}
        }
        // y轴位置
        let bit_p_y = (bit_height) * ele.start + tempHeight / 2 - rittalSize.height/2
        mesh.position.set( 0, bit_p_y , 0 )
        var edges = new THREE.EdgesGeometry(mesh.geometry);
        const line = new THREE.LineSegments( edges, new THREE.LineBasicMaterial( { color: 0x999999 } ) );
        line.position.x = mesh.position.x
        line.position.y = mesh.position.y
        line.position.z = mesh.position.z
        group.add(mesh)
        group.add(line)
    })

    group.add(top_mesh)
    group.add(left_mesh)
    group.add(right_mesh)
    group.add(bottom_mesh)
    group.add(front_mesh)
    group.add(after_mesh)
    group.add(block_mesh)
    group.add(writh_mesh)
    group.add(block2)
    group.add(writh2)
    group.add(block3)
    group.add(writh3)
    group.add(block4)
    group.add(writh4)
    group.add(bit_mesh_01)
    group.add(bit_mesh_02)
    group.add(bit_mesh_03)
    group.add(bit_mesh_04)
    group.userData = {...info}
    return group
}


